package com.bnz.service.impl;

import com.bnz.entity.Classes;
import com.bnz.entity.PageResult;
import com.bnz.entity.Student;
import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.service.StudentService;
import com.bnz.vo.StudentVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    private ClassesMapper classesMapper;

    /**
     * 查
     * @return
     */
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectAll();
//        for (Student student : students) {
//            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
//            student.setCname(classes.getCname());
//        }
        return students;
    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult<Student> findByPage(int page, int pageSize) {
        //开始分页
        PageHelper.startPage(page,pageSize);
        //定义实例
        Example example = new Example(Student.class);
        //开始查询，返回结果
        List<Student> students = studentMapper.selectByExample(example);
        //处理学生关联的班级名称
//        for (Student student : students) {
//            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
//            student.setCname(classes.getCname());
//        }
        Page<Student> page1 = (Page<Student>) students;
        return new PageResult<Student>(page1.getTotal(),page1.getResult());

    }

    /**
     * 条件查询
     * @param page
     * @param pageSize
     * @param vo
     * @return
     */
    @Override
    public PageResult<Student> search(int page, int pageSize, StudentVo vo) {
        //开始分也
        PageHelper.startPage(page,pageSize);
        //定义实例
        Example example = new Example(Student.class);
        //添加查询条件
        Example.Criteria criteria = example.createCriteria();
        //判空
        if (vo != null){
            if (StringUtils.isNotBlank(vo.getSname())){
                criteria.andLike("sname","%"+vo.getSname()+"%");
            }
            if (StringUtils.isNotBlank(vo.getAddr())){
                criteria.andLike("addr","%"+vo.getAddr()+"%");
            }
            if (vo.getCid()!=null && vo.getCid() !=0){
                criteria.andEqualTo("cid",vo.getCid());
        }
        }
        List<Student> students = studentMapper.selectByExample(example);
//        处理学生关联的班级名称
    //        for (Student student : students) {
    //            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
    //            student.setCname(classes.getCname());
    //        }
        //转化对象
        Page<Student> page1 = (Page<Student>) students;

        return new PageResult<Student>(page1.getTotal(),page1.getResult());
    }

    /**
     * 添加
     * @param student
     */
    @Override
    public void save(Student student) {
        studentMapper.insert(student);
    }

    /**
     * 修改
     * @param student
     */
    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }
}
